{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 文件操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 打开文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "filename = \"1.txt\"\n",
    "fr = open(filename)   # filename是文件名,fr是文件句柄"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 读文件内容"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1 全部读到一个字符串中，用于一个文件一个样本的情况"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'11\\t12\\t13\\t14\\ty1\\n21\\t22\\t23\\t24\\ty2\\n31\\t32\\t33\\t34\\ty3\\n41\\t42\\t43\\t44\\ty4'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "filename = \"1.txt\"\n",
    "fr = open(filename) \n",
    "fr.read()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2 得到一个list，每一行占一项，用于一行一个样本的情况"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['11\\t12\\t13\\t14\\ty1\\n',\n",
       " '21\\t22\\t23\\t24\\ty2\\n',\n",
       " '31\\t32\\t33\\t34\\ty3\\n',\n",
       " '41\\t42\\t43\\t44\\ty4']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "filename = \"1.txt\"\n",
    "fr = open(filename) \n",
    "fr.readlines()  # 一次读入所有的内容，并返回list"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "readlines只能用一次，再读就读不到了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "filename = \"1.txt\"\n",
    "fr = open(filename) \n",
    "fr.readlines()  \n",
    "fr.readlines()  # readlines只能用一次，再读就读不到了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['11\\t12\\t13\\t14\\ty1\\n',\n",
       " '21\\t22\\t23\\t24\\ty2\\n',\n",
       " '31\\t32\\t33\\t34\\ty3\\n',\n",
       " '41\\t42\\t43\\t44\\ty4']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fr = open(filename) \n",
    "fr.readlines()  # 重新再open一次，就能重新开始读内容了"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 处理文件内容"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. 依次处理每一行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11\t12\t13\t14\ty1\n",
      "\n",
      "21\t22\t23\t24\ty2\n",
      "\n",
      "31\t32\t33\t34\ty3\n",
      "\n",
      "41\t42\t43\t44\ty4\n"
     ]
    }
   ],
   "source": [
    "filename = \"1.txt\"\n",
    "fr = open(filename) \n",
    "for line in fr.readlines():\n",
    "    print(line)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. 文件中的每一行都以换行符结束，处理时通常要先去掉换行符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11\t12\t13\t14\ty1\n",
      "21\t22\t23\t24\ty2\n",
      "31\t32\t33\t34\ty3\n",
      "41\t42\t43\t44\ty4\n"
     ]
    }
   ],
   "source": [
    "filename = \"1.txt\"\n",
    "fr = open(filename) \n",
    "for line in fr.readlines():   # 依次处理每一行\n",
    "    line = line.strip()       # 去掉行尾的换行符\n",
    "    print(line)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. 通常一行代表一个样本，一行里面包含一个样本的多个特征，多个特征之间用'\\t'格开"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['11', '12', '13', '14', 'y1']\n",
      "['21', '22', '23', '24', 'y2']\n",
      "['31', '32', '33', '34', 'y3']\n",
      "['41', '42', '43', '44', 'y4']\n"
     ]
    }
   ],
   "source": [
    "filename = \"1.txt\"\n",
    "fr = open(filename) \n",
    "for line in fr.readlines():   # 依次处理每一行\n",
    "    line = line.strip()       # 去掉行尾的换行符\n",
    "    listFromLine = line.split('\\t')   # 提取每个特征\n",
    "    print (listFromLine)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. 如果是监督学习，通常一个样本的前n个数据是特征，最后一个是输出标记"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "特征向量：['11', '12', '13', '14']\n",
      "输出标记：y1\n",
      "特征向量：['21', '22', '23', '24']\n",
      "输出标记：y2\n",
      "特征向量：['31', '32', '33', '34']\n",
      "输出标记：y3\n",
      "特征向量：['41', '42', '43', '44']\n",
      "输出标记：y4\n"
     ]
    }
   ],
   "source": [
    "filename = \"1.txt\"\n",
    "fr = open(filename) \n",
    "for line in fr.readlines():   # 依次处理每一行\n",
    "    line = line.strip()       # 去掉行尾的换行符\n",
    "    listFromLine = line.split('\\t')   # 提取每个特征\n",
    "    attr = listFromLine[0:-1]  # 特征向量\n",
    "    label = listFromLine[-1]  # 输出标记\n",
    "    print (\"特征向量：\" + str(attr))\n",
    "    print (\"输出标记：\" + label)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. 把m个数据n个特征存到m*n的矩阵中，输出标记用list保存"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "样本特征矩阵：[[11. 12. 13. 14.]\n",
      " [21. 22. 23. 24.]\n",
      " [31. 32. 33. 34.]\n",
      " [41. 42. 43. 44.]]\n",
      "输出标记列表：['y1', 'y2', 'y3', 'y4']\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "filename = \"1.txt\"\n",
    "fr = open(filename) \n",
    "lines = fr.readlines()\n",
    "numberOfLines = len(lines)\n",
    "\n",
    "returnMat = np.zeros((numberOfLines, 4))    # 初始化样本特征矩阵\n",
    "classLabelVector = []                    # 初始化化输出标记列表\n",
    "index = 0\n",
    "    \n",
    "for line in lines:   # 依次处理每一行\n",
    "    line = line.strip()       # 去掉行尾的换行符\n",
    "    listFromLine = line.split('\\t')   # 提取每个特征\n",
    "    returnMat[index, :] = listFromLine[0:-1]  # 提取特征向量，并加入到特征矩阵中\n",
    "    classLabelVector.append(listFromLine[-1])  # 提取输出标记，并加入输出列表中\n",
    "    \n",
    "    index += 1\n",
    "        \n",
    "print (\"样本特征矩阵：\" + str(returnMat))\n",
    "print (\"输出标记列表：\" + str(classLabelVector))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 读文件夹"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 列出某个路径下的所有文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['.ipynb_checkpoints', '1.txt', 'file.ipynb']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from os import listdir\n",
    "listdir(\"./\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# pickle将对象序列化并存储到文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# pickle将对象序列化成二进制，而不是字符串，所以open参数必须指明'b'\n",
    "def storeTree(inputTree,filename):\n",
    "    import pickle\n",
    "    fw = open(filename,'wb')\n",
    "    pickle.dump(inputTree,fw)\n",
    "    fw.close()\n",
    "def grabTree(filename):\n",
    "    import pickle\n",
    "    fr = open(filename, 'rb')\n",
    "    return pickle.load(fr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'no surfacing': {0: 'no', 1: {'flippers': {0: 'no', 1: 'yes'}}}}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "myTree = {'no surfacing': {0: 'no', 1: {'flippers': {0: 'no', 1: 'yes'}}}}\n",
    "storeTree(myTree,'classifierStorage.txt')\n",
    "grabTree('classifierStorage.txt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
